//
//  17. 电话号码的字母组合(第二遍练习).swift
//  LeetCodeTrain
//
//  Created by rjb on 2021/6/27.
//  Copyright © 2021 rjb. All rights reserved.
//

import Foundation
class Solution17 {
    var result: [String] = []
    var path: [String] = []
    func letterCombinations(_ digits: String) -> [String] {
        let digitsArray = Array(digits)
        guard digitsArray.count > 0 else {
            return result
        }
        backTrace(digits: digitsArray, start: 0)
        return result
    }
    func backTrace(digits: [Character], start: Int) {
        if start > digits.count - 1 {
            result.append(path.joined())
            return
        }
        let digit = digits[start]
        let leters = digitToChar(digit)
        for leter in leters {
            path.append(leter)
            backTrace(digits: digits, start: start + 1)
            path.removeLast()
        }
    }
    func digitToChar(_ digit: Character) -> [String] {
        var letters: [String] = []
        if digit == "2" {
            letters = ["a", "b", "c"]
        } else if digit == "3" {
            letters = ["d", "e", "f"]
        } else if digit == "4" {
            letters = ["g", "h", "i"]
        } else if digit == "5" {
            letters = ["j", "k", "l"]
        } else if digit == "6" {
            letters = ["m", "n", "o"]
        } else if digit == "7" {
            letters = ["p", "q", "r","s"]
        } else if digit == "8" {
            letters = ["t", "u", "v"]
        } else if digit == "9" {
            letters = ["w", "x", "y","z"]
        }
        return letters
    }
    static func test() {
        let solution = Solution17()
        let digits = "23"
        let result = solution.letterCombinations(digits)
        print(result)
    }
}
